package com.to8to.kitt.esb;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.NodeCacheListener;

public abstract class ZKNodeCacheListener implements NodeCacheListener{

	private String path;
	
	private byte[] odata;
	
	private CuratorFramework client;	
	
	public ZKNodeCacheListener(CuratorFramework client, String path)
	{
		this.client = client;
		
		this.path = path;
		
		try {
			odata = client.getData().forPath(path);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	public void nodeChanged() throws Exception {
		
		byte[] ndata = client.getData().forPath(path);
		
		NodeChangeEvent e = new NodeChangeEvent();
		
		e.odata = odata;
		e.ndata = ndata;
		e.path = path;
		
		onNodeChanged(e);
		
		odata = ndata;
	}
	
	
	abstract public void onNodeChanged(NodeChangeEvent e) throws Exception;

}
